home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 June / MacFormat 25.iso / Shareware City / Developers / macgzip_03b2-src / macos / think / ThePrefs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-07  |  21.7 KB  |  775 lines  |  [TEXT/KAHL]

  1. /*            SpinCursors by
  2.  *            America Online: LISPer
  3.  *             Internet: tree@uvm.edu
  4.  */
  5. #ifndef _FOLDERS_
  6. #include <Folders.h>
  7. #endif
  8. #include <Script.h>
  9. #include <Balloons.h>
  10.  
  11. #include <ICAPI.h>
  12. #include <ICKeys.h>
  13.  
  14. #include "ThePrefs.h"
  15. #include "MacErrors.h"
  16. #include "PopUp.h"
  17.  
  18.  
  19. /************************************************************************************/
  20.  
  21. #define kPrefsDialogID        130
  22. #define kSaveButtonItem        1
  23. #define kCancelButtonItem    2
  24.  
  25. #define    kKeepOrig            16
  26. #define kBeepWD                14
  27. #define kQuitWD                5
  28.  
  29. #define kForce                7
  30. #define    kBHelp                8
  31. #define kHelp                15
  32.  
  33. #define kCStKeys            26
  34. #define kUStKeys            27
  35. #define    kAltStKeys            30
  36.  
  37. #define kTEXTsuffix            3
  38. #define kTEXTcreator        4
  39. #define kTEXTbcreator        23
  40. #define kTEXTbtype            28
  41.  
  42. #define kTEXTlevel            10
  43.  
  44. #define kUpArrow            11
  45. #define kDownArrow            12
  46.  
  47. #define    kAsciiGzip            17
  48. #define    kAsciiGunzip        18
  49. #define    kGzip                19
  50.  
  51.  
  52. #define    kAsciiGzipMenu        128
  53. #define    kAsciiGunzipMenu    129
  54. #define    kGzipMenu            130
  55.  
  56. /* prefs file constants */
  57. #define kPrefsNameStrID            128
  58. #define kPrefsCreatorType        '\?\?\?\?'
  59. #define kPrefsFileType            'pref'
  60. #define kPrefsResourceType        'Pref'
  61. #define kPrefsResourceID        128
  62.  
  63. #define kFetchPrefsNameStrID    129
  64. #define kFetchPrefsResourceType    'SUFX'
  65. #define kFetchPrefsResourceID    1
  66.  
  67. #define kFinderMessageStrID    -16397 /* ID of STR for default finder message */
  68.                                    /* see Inside Mac VI, page 9-22         */
  69. #define kStrType            'STR '
  70.  
  71. #define CmdKeyMask            0x0100
  72.  
  73. /*
  74.  * From DialogBits,C.K. Haun
  75.  * Apple Developer Tech Support
  76.  */
  77.  
  78.  /* key equates */
  79. enum  {
  80.     kEnterKey        = 0x03, 
  81.     kTabKey            = 9, 
  82.     kReturnKey        = 0x0D, 
  83.     kBackSpace        = 8, 
  84.     kEscKey            = 0x1B, 
  85.     kLeftArrowKey    = 0x1C, 
  86.     kRightArrowKey, 
  87.     kUpArrowKey,
  88.     kDownArrowKey, 
  89.     kDeleteKey        = 0x7F
  90. };
  91.  
  92. pascal Boolean filterIt(DialogPtr inputDialog, EventRecord *myDialogEvent, short *theDialogItem);
  93.  
  94. pascal OSErr GetStdFilterProc(ModalFilterProcPtr *theProc) = 
  95. {
  96.     0x303C, 0x0203, 0xAA68
  97. };
  98.  
  99. /* Indicates to the dialog manager which item is default.  Will then alias the return key */
  100. /* to this item, and also bold border it for you (yaaaaa!) */
  101. pascal OSErr SetDialogDefaultItem(DialogPtr theDialog, short newItem)
  102. {
  103.     0x303C, 0x0304, 0xAA68
  104. };
  105.  
  106. /* Indicates which item should be aliased to escape or Command - . */
  107. pascal OSErr SetDialogCancelItem(DialogPtr theDialog, short newItem)
  108. {
  109.     0x303C, 0x0305, 0xAA68
  110. };
  111. /* End From DialogBits */
  112.  
  113. /* these constants may be stored in the fields of the prefs struct */
  114.  
  115. static OSErr GetPrefsName (StringPtr prefName, short StrID);
  116. static ICError ICGetPrefHandle (ICInstance inst, ConstStr255Param key, 
  117.                          ICAttr *attr, Handle *prefh);
  118.                          
  119. PrefsType    currPrefs;
  120. Boolean        prefsChangedFlag;
  121.  
  122. Handle        FPrefs;
  123. Size        FPrefsSize=0;
  124.  
  125. ICInstance    inst;
  126. Handle        ICmappings = nil;
  127.  
  128.  
  129. short OpenPrefsResFile(SignedByte prefsPerm, Boolean createFlag, short StrID)
  130. /* open the preferences file with the given permission; if createFlag is set,
  131.    create a preferences file if necessary */
  132. {
  133.     OSErr retCode;
  134.     short prefsVRefNum;
  135.     long prefsDirID;
  136.     Str255 prefsNameStr;
  137.     FSSpec prefsFSSpec;
  138.     short prefsResRefNum;
  139.     
  140.     prefsResRefNum = -1;
  141.     /* get the name of the prefs file */
  142.     retCode = GetPrefsName(prefsNameStr, StrID);
  143.     if (retCode == noErr) {
  144.     
  145.         /* Use FindFolder to locate the Preferences folder.
  146.         
  147.            MPW 3.2 and some other compilers provide the
  148.            glue necessary to use FindFolder (and Gestalt) safely
  149.            under System 6 and earlier.  Under System 6, the dirID
  150.            returned for the Preferences folder request will be
  151.            the System folder's dirID */
  152.  
  153.         retCode = FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
  154.             &prefsVRefNum, &prefsDirID);
  155.         if (retCode == noErr) {
  156.         
  157.             /* make a file spec for the prefs file */
  158.             
  159.             retCode = FSMakeFSSpec(prefsVRefNum, prefsDirID, prefsNameStr,
  160.                 &prefsFSSpec);
  161.                 
  162.             if (retCode == fnfErr && createFlag) {
  163.                 /* prefs file doesn't already exist, so create it */
  164.                 FSpCreateResFile(&prefsFSSpec, kPrefsCreatorType, kPrefsFileType,
  165.                     smSystemScript);
  166.                 retCode = ResError();
  167.             }
  168.             
  169.             /* open the prefs file */
  170.             if (retCode == noErr) {
  171.                 prefsResRefNum = FSpOpenResFile(&prefsFSSpec, prefsPerm);
  172.             }
  173.         }
  174.     }
  175.     return prefsResRefNum;
  176. }
  177.  
  178. static OSErr GetPrefsName (StringPtr prefName, short StrID)
  179. /* get the name of the preferences file from a resource in the application */
  180. {
  181.     StringHandle prefStrHandle;
  182.     OSErr retCode = noErr;
  183.     
  184.     prefStrHandle = GetString(StrID);
  185.     if (prefStrHandle != nil) 
  186.         BlockMove(*prefStrHandle, prefName, (Size) (**prefStrHandle) + 1);
  187.     else retCode = resNotFound;
  188.     
  189.     return retCode;
  190. }
  191.  
  192. Boolean GetOrGeneratePrefs( PrefsType * thePrefsTypePtr, long wantedVers )
  193. /* fill in the prefs structure from the resource file or from scratch */
  194. {
  195.     short                prefsResRefNum;
  196.     Handle                tempHandle;
  197.     extern    Boolean        gSavedPos;
  198.     extern    Point        gSavedPoint;
  199.     
  200.     /* initialize prefs structure in case we can't get a valid set */
  201.     thePrefsTypePtr->prefsTypeVers =        wantedVers;
  202.     thePrefsTypePtr->compress =                false;
  203.     thePrefsTypePtr->ascii =                false;
  204.     thePrefsTypePtr->force =                false;
  205.     thePrefsTypePtr->level =                6;
  206.     thePrefsTypePtr->suffix[0] =            0x00;
  207.     BlockMove("\pttxt", thePrefsTypePtr->textcreator, 5);
  208.     BlockMove("\p\?\?\?\?", thePrefsTypePtr->bincreator, 5);
  209.     BlockMove("\p\?\?\?\?", thePrefsTypePtr->bintype, 5);
  210.     thePrefsTypePtr->KeepOriginals =        false;
  211.     thePrefsTypePtr->QuitWhenDone =            true;
  212.     thePrefsTypePtr->BeepWhenDone =            false;
  213.  
  214.     thePrefsTypePtr->AsciiCompress =        OnlyText;
  215.     thePrefsTypePtr->AsciiUnCompress =        UseMenu;
  216.     thePrefsTypePtr->ResourceCompress =        Ask;
  217.     thePrefsTypePtr->WhenCompress =            NonMacGzip;
  218.  
  219.     thePrefsTypePtr->DecodeHQX =    false;
  220.     thePrefsTypePtr->StKeysComp =    true;
  221.     thePrefsTypePtr->StKeysUnComp =    true;
  222.     thePrefsTypePtr->StKeysAlt =     true;
  223.  
  224.     thePrefsTypePtr->SavedPos = false;
  225.     thePrefsTypePtr->SavedPoint.h = 0;
  226.     thePrefsTypePtr->SavedPoint.v = 0;
  227.     
  228.     
  229.     /* open (but don't create) the prefs file */
  230.     prefsResRefNum = OpenPrefsResFile(fsRdPerm, false, kPrefsNameStrID);
  231.     if (prefsResRefNum != -1) {
  232.     
  233.         /* file opened successfully, get the prefs resource */
  234.         tempHandle = Get1Resource(kPrefsResourceType, kPrefsResourceID);
  235.         
  236.         /* if the resource is there and it's the right size and version, copy it */
  237.         /* (in C these can be combined with &&, but don't use AND in Pascal)     */
  238.         
  239.         if (tempHandle != nil)
  240.             if (GetHandleSize(tempHandle) == sizeof(PrefsType)) 
  241.                 if ((*(PrefsType *)*tempHandle).prefsTypeVers == wantedVers) {
  242.                 
  243.                     /* copy the prefs struct */
  244.                     *thePrefsTypePtr = *(PrefsType *)*tempHandle;
  245.                     
  246.                 }
  247.         /* release the pref resource and close the file */
  248.         CloseResFile(prefsResRefNum);
  249.         
  250.     /*
  251.      * Get Fetch Prefs
  252.      */
  253.      
  254.     if( thePrefsTypePtr->AsciiUnCompress == Fetch )
  255.     {
  256.     if((prefsResRefNum = OpenPrefsResFile(fsRdPerm, false, kFetchPrefsNameStrID))!=-1)
  257.         {
  258.             tempHandle = GetResource('SUFX',1);
  259.             FPrefs = tempHandle;
  260.             HandToHand( &FPrefs );
  261.             FPrefsSize = GetHandleSize(FPrefs);
  262.             ReleaseResource( tempHandle );
  263.             CloseResFile(prefsResRefNum);
  264.         }
  265.         else
  266.         {
  267.             /* Fetch Prefs not found; set to menu */
  268.             Calert("Warning: 'Fetch Prefs' not found. Check your Prefs. folder");
  269.             thePrefsTypePtr->AsciiUnCompress = UseMenu;
  270.         }
  271.     }
  272.     /*
  273.      * Get Internet Config Prefs
  274.      */
  275.     else if (
  276.         ( thePrefsTypePtr->AsciiUnCompress == UseICinExp ) ||
  277.         ( thePrefsTypePtr->AsciiCompress == UseICinComp ) )
  278.     {
  279.         Boolean ICErr = true;
  280.         
  281.         if (!ICStart(&inst, 'ICMa'))
  282.         {                                                // init the sucker
  283.             if (!ICFindConfigFile(inst, 0, nil))
  284.             {                                            // locate the config file
  285.                 if (!ICBegin(inst, icReadOnlyPerm))
  286.                 {                                        // open up the file for reading
  287.                     ICAttr    attr = 0;
  288.                     
  289.                     ICErr = false;
  290.                     
  291.                     if (!ICGetPrefHandle(inst, (ConstStr255Param)kICMapping, &attr, &ICmappings))
  292.                     {
  293.                         HUnlock(ICmappings);
  294.                     }
  295.                     else
  296.                     {
  297.                         /* Can't get ICmappings */
  298.                         ICmappings = nil;
  299.                         Calert("Warning: Can't get Mappings from IC. Check your IC Prefs");
  300.                         thePrefsTypePtr->AsciiUnCompress = UseMenu;
  301.                     }
  302.                     ICEnd(inst);                            // all done
  303.                 }
  304.                 
  305.             }
  306.             ICStop(inst);
  307.         }
  308.         
  309.         
  310.         if(ICErr)
  311.         {
  312.             ICmappings = nil;
  313.             Calert("Warning: IC Prefs not found. Check your Internet Prefs");
  314.             thePrefsTypePtr->AsciiUnCompress = UseMenu;
  315.         }
  316.     }
  317.     
  318.         gSavedPos = thePrefsTypePtr->SavedPos;
  319.         gSavedPoint.h = thePrefsTypePtr->SavedPoint.h;
  320.         gSavedPoint.v = thePrefsTypePtr->SavedPoint.v;
  321.  
  322.     return true;
  323.     
  324.     }
  325.     else
  326.         return false;
  327. }
  328.  
  329.  
  330.  
  331.  
  332.  
  333. OSErr SavePrefs(PrefsType * thePrefsTypePtr )
  334. /* save the prefs structure in the prefs resource file */
  335. {
  336.     OSErr retCode;
  337.     short prefsResRefNum;
  338.     Handle prefHandle, finderMessageHandle;
  339.     
  340.     /* open (and, if necessary, create) the prefs file */
  341.     prefsResRefNum = OpenPrefsResFile(fsRdWrPerm, true, kPrefsNameStrID);
  342.     if (prefsResRefNum != -1) {
  343.     
  344.         /* file opened successfully, get the prefs resource */
  345.         prefHandle = Get1Resource(kPrefsResourceType, kPrefsResourceID);
  346.         
  347.         if (prefHandle == nil) {
  348.         
  349.             /* create a new resource */
  350.             prefHandle = NewHandle(sizeof(PrefsType));
  351.             if (prefHandle != nil) {
  352.             
  353.                 /* copy the prefs struct into the handle
  354.                    and make it into a resource */
  355.                 
  356.                 *(PrefsType *)*prefHandle = *thePrefsTypePtr;
  357.                 AddResource(prefHandle, kPrefsResourceType, kPrefsResourceID, 
  358.                     "\pPrefsType");
  359.                 retCode = ResError();
  360.                 if (retCode != noErr) DisposHandle(prefHandle);
  361.             } 
  362.             
  363.             else retCode = MemError(); /* NewHandle failed */
  364.         }
  365.         
  366.         else {  /* prefHandle != nil */
  367.         
  368.             /* update the existing resource */
  369.             SetHandleSize(prefHandle, sizeof(PrefsType));
  370.             retCode = MemError();
  371.             if (retCode == noErr) {
  372.  
  373.                 /* copy the prefs struct into the handle and tell the rsrc manager */
  374.                 *(PrefsType *)*prefHandle = *thePrefsTypePtr;
  375.                 ChangedResource(prefHandle);
  376.             }
  377.         }
  378.         if (retCode == noErr) {
  379.         
  380.             /* add the message to be displayed if the user tries
  381.                to open the prefs file in the Finder (but don't add it
  382.                if it's already in the preferences file) */
  383.                
  384.             finderMessageHandle = (Handle) GetString(kFinderMessageStrID);
  385.             if (finderMessageHandle != nil &&
  386.                 HomeResFile((Handle) finderMessageHandle) != prefsResRefNum) {
  387.             
  388.                 /* copy the resource into the prefs file */
  389.                 DetachResource(finderMessageHandle);
  390.                 AddResource(finderMessageHandle, kStrType, kFinderMessageStrID,
  391.                     "\pFinder message");
  392.                     
  393.                 /* if AddResource failed, dispose of the handle */
  394.                 retCode = ResError();
  395.                 if (retCode != noErr) DisposHandle(finderMessageHandle);
  396.             }
  397.         }
  398.         
  399.         /* update and close the preference resource file, 
  400.            releasing its resources from memory */
  401.         CloseResFile(prefsResRefNum);
  402.     }
  403.     
  404.     else {
  405.         /* couldn't open the res file */
  406.         retCode = ResError();
  407.         if (retCode == noErr) retCode = resFNotFound;
  408.     }
  409.     
  410.     return retCode;
  411. }
  412.  
  413.  
  414.  
  415. /***********************Dialog*************/
  416. Boolean DoPrefsDialog(PrefsType * thePrefsTypePtr)
  417. /* display the prefs dialog; returns true if change made to prefs record */
  418. {
  419.     GrafPtr savePort;
  420.     DialogPtr prefDialogPtr;
  421.     short item;
  422.     Boolean doneFlag = false;
  423.     Boolean    SaveBalloons;
  424.     OSErr retCode;
  425.  
  426.     /* keep the new preference settings only in local variables
  427.        in case the user cancels */
  428.        
  429.     PrefsType newPrefsType;
  430.  
  431.     short        itemType;
  432.     Handle        itemHandle;
  433.     Rect        itemRect;
  434.     Str255        sTemp;
  435.     
  436.     Boolean changedFlag;
  437.  
  438.  
  439.     /* the old settings are our starting point, so copy them */
  440.     newPrefsType = *thePrefsTypePtr;
  441.     
  442.     /* throw up the dialog */
  443.     prefDialogPtr = GetNewDialog(kPrefsDialogID, nil, (WindowPtr) -1);
  444.     if (prefDialogPtr == nil) ExitToShell();  /* hurtin’ bad */
  445.     
  446.     SetDialogDefaultItem( prefDialogPtr, kSaveButtonItem);
  447.     SetDialogCancelItem( prefDialogPtr, kCancelButtonItem);
  448.  
  449.     /* set up the dialog to the initial values */
  450.     SaveBalloons = HMGetBalloons();
  451.  
  452.     GetDItem(prefDialogPtr, kForce, &itemType, &itemHandle, &itemRect);
  453.     SetCtlValue((ControlHandle) itemHandle, ( newPrefsType.force ? 1 : 0));
  454.  
  455.     GetDItem(prefDialogPtr,kTEXTlevel, &itemType, &itemHandle, &itemRect);
  456.     NumToString( (long)thePrefsTypePtr->level, sTemp );
  457.     SetIText(itemHandle,sTemp);
  458.  
  459.  
  460.  
  461.     GetDItem(prefDialogPtr, kTEXTsuffix, &itemType, &itemHandle, &itemRect);
  462.     SetIText(itemHandle,thePrefsTypePtr->suffix);
  463.  
  464.     
  465.     
  466.     GetDItem(prefDialogPtr, kTEXTcreator, &itemType, &itemHandle, &itemRect);
  467.     SetIText(itemHandle,thePrefsTypePtr->textcreator);
  468.     
  469.     GetDItem(prefDialogPtr, kTEXTbcreator, &itemType, &itemHandle, &itemRect);
  470.     SetIText(itemHandle,thePrefsTypePtr->bincreator);
  471.  
  472.     GetDItem(prefDialogPtr, kTEXTbtype, &itemType, &itemHandle, &itemRect);
  473.     SetIText(itemHandle,thePrefsTypePtr->bintype);
  474.  
  475.     SelIText(prefDialogPtr, kTEXTbcreator, 0, 32767);
  476.  
  477.     GetDItem(prefDialogPtr, kKeepOrig, &itemType, &itemHandle, &itemRect);
  478.     SetCtlValue((ControlHandle) itemHandle, ( newPrefsType.KeepOriginals ? 1 : 0));
  479.  
  480.     GetDItem(prefDialogPtr, kBeepWD, &itemType, &itemHandle, &itemRect);
  481.     SetCtlValue((ControlHandle) itemHandle, ( newPrefsType.BeepWhenDone ? 1 : 0));
  482.  
  483.     GetDItem(prefDialogPtr, kQuitWD, &itemType, &itemHandle, &itemRect);
  484.     SetCtlValue((ControlHandle) itemHandle, ( newPrefsType.QuitWhenDone ? 1 : 0));
  485.  
  486.  
  487.     GetDItem(prefDialogPtr, kAsciiGzip, &itemType, &itemHandle, &itemRect);
  488.     PopUp[kAsciiGzip-PopUpBaseItem].MenuRect = itemRect;
  489.     PopUp[kAsciiGzip-PopUpBaseItem].MenuID = kAsciiGzipMenu;
  490.     PopUp[kAsciiGzip-PopUpBaseItem].Menu = GetMenu(kAsciiGzipMenu);
  491.     PopUp[kAsciiGzip-PopUpBaseItem].Selection = newPrefsType.AsciiCompress;
  492.     SetItemMark(PopUp[kAsciiGzip-PopUpBaseItem].Menu,PopUp[kAsciiGzip-PopUpBaseItem].Selection,checkMark);
  493.     SetDItem(prefDialogPtr, kAsciiGzip, itemType, (ProcPtr) &DrawPopUp, &itemRect);
  494.  
  495. /*    WARNING!
  496.  *    SetDialogItem( prefDialogPtr, kAsciiGzip,
  497.  *                itemType, UserItemUPP((ProcPtr) &DrawPopUp), &itemRect); for PPC ?
  498.  */
  499.     GetDItem(prefDialogPtr, kAsciiGunzip, &itemType, &itemHandle, &itemRect);
  500.     PopUp[kAsciiGunzip-PopUpBaseItem].MenuRect = itemRect;
  501.     PopUp[kAsciiGunzip-PopUpBaseItem].MenuID = kAsciiGunzipMenu;
  502.     PopUp[kAsciiGunzip-PopUpBaseItem].Menu = GetMenu(kAsciiGunzipMenu);
  503.     PopUp[kAsciiGunzip-PopUpBaseItem].Selection = newPrefsType.AsciiUnCompress;
  504.     SetItemMark(PopUp[kAsciiGunzip-PopUpBaseItem].Menu,PopUp[kAsciiGunzip-PopUpBaseItem].Selection,checkMark);
  505.     SetDItem(prefDialogPtr, kAsciiGunzip, itemType, (ProcPtr) &DrawPopUp, &itemRect);
  506.  
  507.     GetDItem(prefDialogPtr, kGzip, &itemType, &itemHandle, &itemRect);
  508.     PopUp[kGzip-PopUpBaseItem].MenuRect = itemRect;
  509.     PopUp[kGzip-PopUpBaseItem].MenuID = kGzipMenu;
  510.     PopUp[kGzip-PopUpBaseItem].Menu = GetMenu(kGzipMenu);
  511.     PopUp[kGzip-PopUpBaseItem].Selection = newPrefsType.WhenCompress;
  512.     SetItemMark(PopUp[kGzip-PopUpBaseItem].Menu,PopUp[kGzip-PopUpBaseItem].Selection,checkMark);
  513.     SetDItem(prefDialogPtr, kGzip, itemType, (ProcPtr) &DrawPopUp, &itemRect);
  514.  
  515.     GetDItem(prefDialogPtr, kCStKeys, &itemType, &itemHandle, &itemRect);
  516.     SetCtlValue((ControlHandle) itemHandle, ( newPrefsType.StKeysComp ? 1 : 0));
  517.     
  518.     GetDItem(prefDialogPtr, kUStKeys, &itemType, &itemHandle, &itemRect);
  519.     SetCtlValue((ControlHandle) itemHandle, ( newPrefsType.StKeysUnComp ? 1 : 0));
  520.     
  521.     GetDItem(prefDialogPtr, kAltStKeys, &itemType, &itemHandle, &itemRect);
  522.     SetCtlValue((ControlHandle) itemHandle, ( newPrefsType.StKeysAlt ? 1 : 0));
  523.  
  524.     GetDItem(prefDialogPtr, kHelp, &itemType, &itemHandle, &itemRect);
  525.     SetCtlValue((ControlHandle) itemHandle, SaveBalloons);
  526.  
  527.     do {
  528.         GetPort(&savePort);
  529.         SetPort(prefDialogPtr);
  530.  
  531.         ModalDialog((ModalFilterProcPtr)filterIt, &item);
  532.         switch(item) {
  533.         
  534.             
  535.             case kSaveButtonItem:
  536.                 /* user wants to save the current or new settings */
  537.                 
  538.                 /* copy the editText string into the new prefs record */
  539.                 GetDItem(prefDialogPtr, kTEXTcreator, &itemType, &itemHandle, &itemRect);
  540.                 GetIText(itemHandle, newPrefsType.textcreator);
  541.                 
  542.                 GetDItem(prefDialogPtr, kTEXTbcreator, &itemType, &itemHandle, &itemRect);
  543.                 GetIText(itemHandle, newPrefsType.bincreator);
  544.                 
  545.                 GetDItem(prefDialogPtr, kTEXTbtype, &itemType, &itemHandle, &itemRect);
  546.                 GetIText(itemHandle, newPrefsType.bintype);
  547.  
  548.                 GetDItem(prefDialogPtr, kTEXTsuffix, &itemType, &itemHandle, &itemRect);
  549.                 GetIText(itemHandle, newPrefsType.suffix);
  550.                 
  551.                 if(newPrefsType.textcreator[0]!=0x04)
  552.                 {
  553.                     Calert( "Text creator not valid (Only 4 chars!)" );
  554.                     SelIText(prefDialogPtr, kTEXTcreator, 0, 32767);
  555.                 }
  556.                 else if (newPrefsType.bincreator[0]!=0x04)
  557.                 {
  558.                     Calert( "Binary creator not valid (Only 4 chars!)" );
  559.                     SelIText(prefDialogPtr, kTEXTbcreator, 0, 32767);
  560.                 }
  561.                 else if (newPrefsType.bintype[0]!=0x04)
  562.                 {
  563.                     Calert( "Binary type not valid (Only 4 chars!)" );
  564.                     SelIText(prefDialogPtr, kTEXTbtype, 0, 32767);
  565.                 }
  566.                 else
  567.                 {
  568.                         /* replace the old record with the new record */
  569.                         
  570.                         newPrefsType.AsciiCompress = PopUp[kAsciiGzip-PopUpBaseItem].Selection;
  571.                         newPrefsType.AsciiUnCompress = PopUp[kAsciiGunzip-PopUpBaseItem].Selection;
  572.                         newPrefsType.WhenCompress = PopUp[kGzip-PopUpBaseItem].Selection;
  573.                         
  574.                         *thePrefsTypePtr = newPrefsType;
  575.                         doneFlag = true;
  576.                 }
  577.                 
  578.                 if (
  579.                     (( FPrefsSize==0) && (newPrefsType.AsciiUnCompress==Fetch)) ||
  580.                     (( ICmappings==nil) && (newPrefsType.AsciiCompress==UseICinComp)) ||
  581.                     (( ICmappings==nil) && (newPrefsType.AsciiUnCompress==UseICinExp))
  582.                     )
  583.                     
  584.                 {
  585.                     Calert("You must restart MacGzip for your changes to take effect");
  586.                 }
  587.                 break;
  588.                 
  589.             case kCancelButtonItem:
  590.                 /* abandon the new preferences structure */
  591.                 doneFlag = true;
  592.                 break;
  593.                 
  594.             case kForce:
  595.             
  596.                 newPrefsType.force = !newPrefsType.force;
  597.                 GetDItem(prefDialogPtr, kForce, &itemType, &itemHandle, &itemRect);
  598.                 SetCtlValue((ControlHandle) itemHandle, ( newPrefsType.force ? 1 : 0));
  599.                 break;
  600.                 
  601.             case kUpArrow:
  602.             
  603.                 if ( newPrefsType.level < 9 )
  604.                 {
  605.                     newPrefsType.level++;
  606.                     GetDItem(prefDialogPtr,kUpArrow, &itemType, &itemHandle, &itemRect);
  607.                     InvertRect(&itemRect);
  608.                     while (StillDown())
  609.                     ;
  610.                     InvertRect(&itemRect);
  611.                     GetDItem(prefDialogPtr,kTEXTlevel, &itemType, &itemHandle, &itemRect);
  612.                     NumToString( (long)newPrefsType.level, sTemp );
  613.                     SetIText(itemHandle,sTemp);
  614.                 }
  615.                 break;
  616.                 
  617.             case kDownArrow:
  618.             
  619.                 if ( newPrefsType.level > 1 )
  620.                 {
  621.                     newPrefsType.level--;
  622.                     GetDItem(prefDialogPtr,kDownArrow, &itemType, &itemHandle, &itemRect);
  623.                     InvertRect(&itemRect);
  624.                     while (StillDown())
  625.                     ;
  626.                     InvertRect(&itemRect);
  627.                     GetDItem(prefDialogPtr,kTEXTlevel, &itemType, &itemHandle, &itemRect);
  628.                     NumToString( (long)newPrefsType.level, sTemp );
  629.                     SetIText(itemHandle,sTemp);
  630.                 }
  631.                 break;
  632.                 
  633.             case kKeepOrig:
  634.             
  635.                 newPrefsType.KeepOriginals = !newPrefsType.KeepOriginals;
  636.                 GetDItem(prefDialogPtr, kKeepOrig, &itemType, &itemHandle, &itemRect);
  637.                 SetCtlValue((ControlHandle) itemHandle, ( newPrefsType.KeepOriginals ? 1 : 0));
  638.                 break;
  639.  
  640.             case kQuitWD:
  641.             
  642.                 newPrefsType.QuitWhenDone = !newPrefsType.QuitWhenDone;
  643.                 GetDItem(prefDialogPtr, kQuitWD, &itemType, &itemHandle, &itemRect);
  644.                 SetCtlValue((ControlHandle) itemHandle, ( newPrefsType.QuitWhenDone ? 1 : 0));
  645.                 break;
  646.                 
  647.             case kBeepWD:
  648.             
  649.                 newPrefsType.BeepWhenDone = !newPrefsType.BeepWhenDone;
  650.                 GetDItem(prefDialogPtr, kBeepWD, &itemType, &itemHandle, &itemRect);
  651.                 SetCtlValue((ControlHandle) itemHandle, ( newPrefsType.BeepWhenDone ? 1 : 0));
  652.                 break;
  653.                 
  654.             case kAsciiGzip:
  655.             case kAsciiGunzip:
  656.             case kGzip:
  657.             
  658.                 DoPopUp(prefDialogPtr, item);
  659.                 break;
  660.             
  661.             case kCStKeys:
  662.             
  663.                 newPrefsType.StKeysComp = !newPrefsType.StKeysComp;
  664.                 GetDItem(prefDialogPtr, kCStKeys, &itemType, &itemHandle, &itemRect);
  665.                 SetCtlValue((ControlHandle) itemHandle, ( newPrefsType.StKeysComp ? 1 : 0));
  666.                 break;
  667.                 
  668.             case kUStKeys:
  669.             
  670.                 newPrefsType.StKeysUnComp = !newPrefsType.StKeysUnComp;
  671.                 GetDItem(prefDialogPtr, kUStKeys, &itemType, &itemHandle, &itemRect);
  672.                 SetCtlValue((ControlHandle) itemHandle, ( newPrefsType.StKeysUnComp ? 1 : 0));
  673.                 break;
  674.  
  675.             case kAltStKeys:
  676.             
  677.                 newPrefsType.StKeysAlt = !newPrefsType.StKeysAlt;
  678.                 GetDItem(prefDialogPtr, kAltStKeys, &itemType, &itemHandle, &itemRect);
  679.                 SetCtlValue((ControlHandle) itemHandle, ( newPrefsType.StKeysAlt ? 1 : 0));
  680.                 break;
  681.  
  682.             case kHelp:
  683.             case kBHelp:
  684.             
  685.                 HMSetBalloons(!HMGetBalloons());
  686.                 GetDItem(prefDialogPtr, kHelp, &itemType, &itemHandle, &itemRect);
  687.                 SetCtlValue((ControlHandle) itemHandle, HMGetBalloons());
  688.                 break;
  689.  
  690.                 
  691.         }
  692.     } while (!doneFlag);
  693.     
  694.     HMSetBalloons(SaveBalloons);
  695.     SetItemMark(PopUp[kAsciiGzip-PopUpBaseItem].Menu,PopUp[kAsciiGzip-PopUpBaseItem].Selection,noMark);
  696.     SetItemMark(PopUp[kAsciiGunzip-PopUpBaseItem].Menu,PopUp[kAsciiGunzip-PopUpBaseItem].Selection,noMark);
  697.     SetItemMark(PopUp[kGzip-PopUpBaseItem].Menu,PopUp[kGzip-PopUpBaseItem].Selection,noMark);
  698.  
  699.     DisposDialog(prefDialogPtr);
  700.     
  701.     SetPort(savePort);
  702.  
  703.     /* if the user clicked Save, return true */
  704.     return (item == kSaveButtonItem);
  705. }
  706.  
  707. pascal Boolean filterIt(DialogPtr inputDialog, EventRecord *myDialogEvent, short *theDialogItem)
  708. {
  709.     ModalFilterProcPtr theModalProc;
  710.     Boolean returnVal = false;
  711.     char theKey;
  712.     
  713.     if (((myDialogEvent->what == keyDown) || (myDialogEvent->what == autoKey)) &&
  714.         (myDialogEvent->modifiers & CmdKeyMask))
  715.     {
  716.         theKey = myDialogEvent->message & charCodeMask;
  717.         switch (theKey)
  718.         {
  719.             case kUpArrowKey:
  720.                             *theDialogItem = kUpArrow;
  721.                             returnVal = true;
  722.                             break;
  723.             case kDownArrowKey:
  724.                             *theDialogItem = kDownArrow;
  725.                             returnVal = true;
  726.                             break;
  727.         }
  728.     }
  729.     
  730.     if (!returnVal)
  731.         if (GetStdFilterProc(&theModalProc) == noErr)
  732.             returnVal=theModalProc(inputDialog, myDialogEvent, theDialogItem);
  733.  
  734.     return(returnVal);
  735. }
  736.  
  737. static ICError ICGetPrefHandle (ICInstance inst, ConstStr255Param key, 
  738.                          ICAttr *attr, Handle *prefh)
  739. {
  740.     ICError    err = noErr;
  741.     long    prefsize = 0;
  742.  
  743.     (*prefh) = 0L;
  744.     err = ICGetPref(inst, key, attr, nil, &prefsize);
  745.     if (err != noErr)    prefsize = 0;
  746.  
  747.     (*prefh) = NewHandle(prefsize);
  748.     err = MemError();
  749.     if (err == noErr) {
  750.         HLock(*prefh);
  751.         err = ICGetPref(inst, key, attr, *(*prefh), &prefsize);
  752.         if (err == icPrefNotFoundErr) {
  753.             attr = 0;
  754.             err = noErr;
  755.         }
  756.         HUnlock(*prefh);
  757.     }
  758.     
  759.     if (err != noErr) {
  760.         if (*prefh) DisposeHandle(*prefh);
  761.         (*prefh) = nil;
  762.     }
  763.     return(err);
  764. }
  765.  
  766.  
  767.  
  768.  
  769.  
  770.  
  771.  
  772.  
  773.